﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class ProjectClassDAO : IProjectClassDAO
    {
        public bool DeleteProjectClass(ProjectClass clsProjectClass)
        {
            return clsProjectClass.Save("D");
        }

        public IList<ProjectClass> GetProjectClass(ProjectClass clsProjectClass)
        {
            DataTable dt = Db.GetDataTable(clsProjectClass.CreateGetCommandParameters());
            clsProjectClass.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("ProjectClass doesn't exist");
            }
            IList<ProjectClass> list = new List<ProjectClass>();
            list.Add(clsProjectClass);
            return list;
        }

        public IList<ProjectClass> GetProjectClass(string projectClass)
        {
            ProjectClass clsProjectClass = new ProjectClass();
            clsProjectClass._ProjectClass = projectClass;
            return this.GetProjectClass(clsProjectClass);
        }

        public IList<ProjectClass> GetsProjectClass()
        {
            ProjectClass clsProjectClass = new ProjectClass();
            IDataReader dtReader = Db.GetDataReader(clsProjectClass.CreateGetsCommandParameters());
            IList<ProjectClass> list = new List<ProjectClass>();
            while (dtReader.Read())
            {
                clsProjectClass = new ProjectClass();
                clsProjectClass.LoadDataToCollection(dtReader);
                list.Add(clsProjectClass);
            }
            return list;
        }

        public DataTable GetsProjectClass(string SPName, DbParameter[] param)
        {
            ProjectClass clsProjectClass = new ProjectClass();
            return Db.GetDataTable(clsProjectClass.GetsDataReader(SPName, param));
        }

        public bool InsertProjectClass(ProjectClass clsProjectClass)
        {
            return clsProjectClass.Save("I");
        }

        public int InsertProjectClassReturnIdentity(ProjectClass clsProjectClass)
        {
            return clsProjectClass.Save("I", true);
        }

        public string InsertProjectClassReturnOutput(ProjectClass clsProjectClass)
        {
            return clsProjectClass.Save("I", "@ProjectClassID");
        }

        public IList<ProjectClass> ListProjectClass(ProjectClass clsProjectClass)
        {
            IDataReader dtReader = Db.GetDataReader(clsProjectClass.CreateListCommandParameters());
            IList<ProjectClass> list = new List<ProjectClass>();
            while (dtReader.Read())
            {
                ProjectClass clsProjectClasss = new ProjectClass();
                clsProjectClasss.LoadDataToCollection(dtReader);
                list.Add(clsProjectClasss);
            }
            return list;
        }

        public IList<ProjectClass> ListProjectClass(string projectClass)
        {
            ProjectClass clsProjectClass = new ProjectClass();
            clsProjectClass._ProjectClass = projectClass;
            return this.ListProjectClass(clsProjectClass);
        }

        public bool UpdateProjectClass(ProjectClass clsProjectClass)
        {
            return clsProjectClass.Save("U");
        }

        public int UpdateProjectClass(string SPName, DbParameter[] param)
        {
            ProjectClass clsProjectClass = new ProjectClass();
            return Db.ExecuteNonQuery(clsProjectClass.UpdateQuery(SPName, param));
        }
    }
}

